package com.community.web.controller.fq;

import com.community.common.annotation.Log;
import com.community.common.core.controller.BaseController;
import com.community.common.core.domain.AjaxResult;
import com.community.common.core.page.TableDataInfo;
import com.community.common.enums.BusinessType;
import com.community.common.utils.DateUtils;
import com.community.common.utils.StringUtils;
import com.community.domain.fq.csd_fee;
import com.community.domain.fq.csd_parking;
import com.community.service.HouseService;
import com.community.service.fq.FeeService;
import com.community.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/fee")
public class FeeController extends BaseController {
    @Autowired
    private FeeService feeService;
    @Autowired
    private  ISysDeptService sysDeptService;
    @Autowired
    private HouseService houseService;

    @PreAuthorize("@ss.hasPermi('fee:fee:list')")
    @GetMapping("/list")
    public TableDataInfo list(csd_fee fee) {
        startPage();
        List<csd_fee> list=feeService.selectFeeList(fee);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('fee:fee:remove')")
    @DeleteMapping("/{id}")
    public AjaxResult delFee(@PathVariable String id) {
        if(StringUtils.isEmpty(id)){
            return AjaxResult.error("参数为空");
        }
        return toAjax(feeService.delFee(Integer.valueOf(id)));
    }

    @PreAuthorize("@ss.hasPermi('fee:fee:add')")
    @Log(title = "增加", businessType = BusinessType.INSERT)
    @PostMapping("/addFee")
    public AjaxResult addFee(@Validated @RequestBody csd_fee fee) {
       long deptId=Long.parseLong(fee.getDeptId());
       String houseNum=fee.getHouseNum();
        if(sysDeptService.selectDeptById(deptId)==null){
            return error("社区ID'"+deptId+"'不存在");}
        if(houseService.checkHouseNum(houseNum)==null){
                 return error("门牌号'"+houseNum+"'不存在");
        }
           String dept=String.valueOf(deptId);
           String  feeType=fee.getFeeType();
        if(feeService.deptIDselect(feeType,dept,houseNum)!=null){
            return error("该户此类型的物业缴费已存在");
        }

        fee.setCreateTime(DateUtils.getNowDate());
        fee.setCreateBy(getUsername());
        return toAjax(feeService.addFee(fee));}

    @PreAuthorize("@ss.hasPermi('fee:fee:edit')")
    @Log(title ="修改", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult upfee(@Validated @RequestBody csd_fee fee)
    {
        long deptId=Long.parseLong(fee.getDeptId());
        String houseNum=fee.getHouseNum();
        if(sysDeptService.selectDeptById(deptId)==null){
            return error("社区ID'"+deptId+"'不存在");}
         else if(houseService.checkHouseNum(houseNum)==null){
            return error("门牌号'"+houseNum+"'不存在");
        }
         else{
            String dept=String.valueOf(deptId);
            String  feeType=fee.getFeeType();
            if(feeService.deptIDselect(feeType,dept,houseNum)!=null&&feeService.deptIDselect(feeType,dept,houseNum).getId()!=fee.getId()){
                return error("该户此类型的物业缴费已存在");
            }

        }

        return toAjax(feeService.updateFee(fee));
    }
    @PreAuthorize("@ss.hasPermi('fee:fee:query')")
    @GetMapping("/query/{id}")
    public AjaxResult authRole(@PathVariable("id") int id)
    {
        AjaxResult ajax = AjaxResult.success();
        csd_fee fee = feeService.selectFeeInfoById(id);
        ajax.put("fee", fee);
        return ajax;
    }

}

